package com.ifinc.common.utils.sec;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;

/**
 * User: luotao
 * Date: 13-11-20
 * Time: 18:45
 */
public class SignUtils {
    private static final String A = "SHA1withDSA";

    private static final String P = "SUN";

    public static boolean verifyDSAFileObjectKey(File objectKeyFilePath
            , File signFilePath
            , File dataFile) throws Exception {


        FileInputStream inputStream = FileUtils.openInputStream(objectKeyFilePath);
        ObjectInputStream oinputStream = new ObjectInputStream(inputStream);
        Object obj = oinputStream.readObject();

        PublicKey pubKey = (PublicKey) obj;

         /* input the signature bytes */
        byte[] sigToVerify = FileUtils.readFileToByteArray(signFilePath);

       /*
        * create a Signature object and initialize it with the public
        * key
        */
        Signature sig = Signature.getInstance(A, P);
        sig.initVerify(pubKey);

       /* Update and verify the data */
        byte[] dataByte = FileUtils.readFileToByteArray(dataFile);
        sig.update(dataByte);
        boolean verifies = sig.verify(sigToVerify);
        return verifies;
    }

    public static byte[] signTextWithRSA(String keyPath, String keyPass, String src) throws Exception {

        FileInputStream fis = new FileInputStream(keyPath);
        PrivateKey privateKey = new KeyManager().readPrivateKeyfromPKCS12StoredFile(fis, keyPass);

        java.security.Signature signet = java.security.Signature
                .getInstance("MD5withRSA");
        signet.initSign(privateKey);
        signet.update(src.getBytes("ISO-8859-1"));
        byte[] signed = signet.sign();
        return signed;
    }

    public static boolean verifySignTextWithRSA(String pubKeyPath,String srcStr, byte[] src) throws Exception {
        FileInputStream fis = new FileInputStream(pubKeyPath);
        PublicKey publicKey = (PublicKey) new KeyManager().fromCerStoredFile(fis);

        java.security.Signature signet = java.security.Signature
                .getInstance("MD5withRSA");
        signet.initVerify(publicKey);
        signet.update(srcStr.getBytes());
        return signet.verify(src);
    }

    public static void main(String[] args) throws Exception {
        String keyFilePath = "E:\\project\\work\\bank\\bank-check\\bank-check-resolver\\src\\main\\java\\com\\wangyin\\bank\\check\\resolver\\security\\public_wyzx_test.key";
        String signFilePath = "E:\\project\\work\\bank\\bank-check\\bank-check-resolver\\src\\main\\java\\com\\wangyin\\bank\\check\\resolver\\security\\signature.sgn.20111119";
        String dataFilePath = "E:\\project\\work\\bank\\bank-check\\bank-check-resolver\\src\\main\\java\\com\\wangyin\\bank\\check\\resolver\\security\\zdz_wyzx_20121026_1.txt.pgp";
        boolean result = verifyDSAFileObjectKey(new File(keyFilePath), new File(signFilePath), new File(dataFilePath));
        System.out.println("result = " + result);

    }
}
